package com.jcl.core.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils extends org.apache.commons.lang3.time.DateUtils {

    public static final String DATE_FORMAT_YMDHMS           = "yyyy-MM-dd HH:mm:ss";
    public static final String DATE_FORMAT_YMDHM            = "yyyy-MM-dd HH:mm";
    public static final String DATE_FORMAT_YMD              = "yyyy-MM-dd";

    public static final String DATE_FORMAT_YMDHMS_CH        = "yyyy年MM月dd HH:mm:ss";
    public static final String DATE_FORMAT_YMDHM_CH         = "yyyy年MM月dd HH:mm";
    public static final String DATE_FORMAT_YMD_CH           = "yyyy年MM月dd";

    public static String format(Date date, String pattern){
        if(null == date){
            return null;
        }
        if(null == pattern){
            pattern = DATE_FORMAT_YMDHMS;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(pattern);
        return sdf.format(date);
    }

    public static String format(Date date){
        return format(date, null);
    }


    public static Date charToDate(String dateStr, String format) throws ParseException {

        if(null == dateStr || null == format){
            return null;
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.parse(dateStr);
    }

    public static Long getDateTime(Date date, int days){
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.DATE, days);
        return c.getTime().getTime();
    }

    public static Integer formatAge(String day){

        if(null == day){
            return null;
        }

        String format = null;
        if(day.length() > 19){
            day = day.substring(0, 19);
            format = DATE_FORMAT_YMDHMS;
        }else if(day.length() > 10 && day.length() < 19){
            day = day.substring(0, 10);
            format = DATE_FORMAT_YMD;
        }else if(day.length() == 10){
            format = DATE_FORMAT_YMD;
        }
        Date birthDay = null;
        try {
            birthDay = charToDate(day, format);

            return formatAge(birthDay);
        } catch (ParseException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static Integer formatAge(Date birthDay){
        Calendar cal = Calendar.getInstance();

        if (cal.before(birthDay)) {
            return null;
        }
        int yearNow = cal.get(Calendar.YEAR);
        int monthNow = cal.get(Calendar.MONTH);
        int dayOfMonthNow = cal.get(Calendar.DAY_OF_MONTH);
        cal.setTime(birthDay);

        int yearBirth = cal.get(Calendar.YEAR);
        int monthBirth = cal.get(Calendar.MONTH);
        int dayOfMonthBirth = cal.get(Calendar.DAY_OF_MONTH);

        int age = yearNow - yearBirth;
        if (monthNow <= monthBirth) {
            if (monthNow == monthBirth) {
                if (dayOfMonthNow < dayOfMonthBirth) age--;
            }else{
                age--;
            }
        }
        return age;

    }

    public static void main(String[] args) {
        System.out.println(addDays(new Date(), -1));
    }
}
